package com.biz.primus.ms.tms.service;

import com.biz.primus.base.enums.CommonStatus;
import com.biz.primus.common.utils.CopyUtils;
import com.biz.primus.model.tms.vo.UpgradeAddReqVo;
import com.biz.primus.model.tms.vo.UpgradeDetailVo;
import com.biz.primus.model.tms.vo.UpgradeRespVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.tms.dao.po.UpgradePo;
import com.biz.primus.ms.tms.dao.repository.UpgradeRepository;
import com.biz.primus.ms.tms.trans.UpgradePoToUpgradeResVo;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * UpgradeService
 *
 * @author JKLiues
 * @date 2017/11/21
 */
@Service
public class UpgradeService extends AbstractBaseService {

    @Autowired
    private UpgradeRepository upgradeRepository;

    public UpgradeRespVo needUpgrade(String os, String version) {
        List<UpgradePo> upgradePos = upgradeRepository.findByOsAndStatus(os, CommonStatus.ENABLE);
        if (CollectionUtils.isNotEmpty(upgradePos)) {
            for (UpgradePo upgradePo : upgradePos) {
                if (upgradePo.versionToScore() > UpgradePo.versionToScore(version)) {
                    return new UpgradePoToUpgradeResVo().apply(upgradePo);
                }
            }

        }
        return null;
    }

    public List<UpgradeDetailVo> findAll() {
        List<UpgradePo> all = upgradeRepository.findAll();
        return Optional.ofNullable(all).orElse(Lists.newArrayList())
                .stream()
                .map(po -> {
                    UpgradeDetailVo vo = new UpgradeDetailVo();
                    CopyUtils.copyProperties(po, vo);
                    return vo;
                }).collect(Collectors.toList());
    }


    public List<UpgradeDetailVo> findByOs(String os) {
        List<UpgradePo> all = upgradeRepository.findByOsAndStatus(os, CommonStatus.ENABLE);
        return Optional.ofNullable(all).orElse(Lists.newArrayList())
                .stream()
                .map(po -> {
                    UpgradeDetailVo vo = new UpgradeDetailVo();
                    CopyUtils.copyProperties(po, vo);
                    return vo;
                }).collect(Collectors.toList());
    }

    public void disable(Long id) {
        UpgradePo upgradePo = upgradeRepository.findOne(id);
        upgradePo.setStatus(CommonStatus.DISABLE);
        upgradeRepository.save(upgradePo);
    }

    /**
     * 添加升级版本
     *
     * @param upgrade
     */
    @Transactional
    public void save(UpgradeAddReqVo upgrade) {
        UpgradePo upgradePo = new UpgradePo();
        upgradePo.setId(idService.getNextId());
        upgradePo.setOs(upgrade.getOs());
        upgradePo.setIsForce(upgrade.isForce());
        upgradePo.setVersion(upgrade.getVersion());
        upgradePo.setInfo(upgrade.getInfo());
        upgradePo.setUrl(upgrade.getUrl());
        upgradePo.setMd5(upgrade.getMd5());
        upgradePo.setInHouse(upgrade.getInHouse());
        upgradePo.setStatus(CommonStatus.ENABLE);
        upgradePo.setCreateTimestamp(new Timestamp(System.currentTimeMillis()));
        upgradeRepository.save(upgradePo);
    }

}
